package com.haimi.shiguang.utils;


import android.util.Log;

import com.haimi.shiguang.bean.BusinessBean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by chris on 16/6/7.
 */
public class ListUtil {

    public static boolean isEmpty(List<?> list) {
        return list == null || list.isEmpty();
    }

    public static boolean listIsEmpty(List list) {
        return list == null || list.isEmpty();
    }

    public static boolean arrayIsEmpty(Object[] objects) {
        return objects == null || objects.length == 0;
    }

    public static <T extends BusinessBean> List<T> cloneList(List<T> list) {
        if (list == null) {
            return new ArrayList(0);
        } else {
            ArrayList cloneList = new ArrayList(list.size());

            try {
                Iterator e = list.iterator();

                while (e.hasNext()) {
                    BusinessBean t = (BusinessBean) e.next();
                    if (t != null) {
                        cloneList.add(t.clone());
                    }
                }
            } catch (CloneNotSupportedException var4) {
                var4.printStackTrace();
            }

            return cloneList;
        }
    }

    public static List<String> cloneStringList(List<String> list) {
        if (list == null) {
            return new ArrayList();
        } else {
            ArrayList cloneList = new ArrayList(list.size());
            Iterator var2 = list.iterator();

            while (var2.hasNext()) {
                String t = (String) var2.next();
                if (t != null) {
                    cloneList.add(t);
                }
            }

            return cloneList;
        }
    }

    /**
     * 深度克隆list
     *
     * @param src
     * @param <T>
     * @return
     */
    public static <T> List<T> deepCopyList(List<T> src) {
        List<T> dest = null;
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            dest = (List<T>) in.readObject();
        } catch (Exception e) {
            Log.e("ListUtil", e.getMessage());
        }
        return dest;
    }
}
